package com.snow.useviewdemo.util.phone;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;

import androidx.annotation.NonNull;

import com.snow.baselibrary.utils.PhoneRomUtil;

/**
 * Created by zhaoyingtao
 * Date: 2020/10/15
 * Describe:跳转手机的自启管理页面
 */
public class PhoneStartWhitelistUtils {
    private volatile static PhoneStartWhitelistUtils appUtils;

    public static PhoneStartWhitelistUtils getInstance() {
        if (appUtils == null) {
            synchronized (PhoneStartWhitelistUtils.class) {
                if (appUtils == null) {
                    appUtils = new PhoneStartWhitelistUtils();
                }
            }
        }
        return appUtils;
    }

    /**
     * 跳转手机自启管理页面
     *
     * @param mActivity
     */
    public void goToPhoneStartWhitelisActivity(Activity mActivity) {
        if (PhoneRomUtil.isXiaoMi()) {
            goXiaomiSetting(mActivity);
        } else if (PhoneRomUtil.isHuaWei()) {
            goHuaweiSetting(mActivity);
        } else if (PhoneRomUtil.isOppo()) {
            goOPPOSetting(mActivity);
        } else if (PhoneRomUtil.isMeizu()) {
            goMeizuSetting(mActivity);
        } else if (PhoneRomUtil.isVivo()) {
            goVIVOSetting(mActivity);
        } else if (PhoneRomUtil.isSamsung()) {
            goSamsungSetting(mActivity);
        } else {//其他手机跳转应用设置页面
            goToAppDetailSetting(mActivity);
        }
    }

    /***********************跳转各手机品牌的自启管理==开始********************************/
    public void goXiaomiSetting(Activity mActivity) {
        showActivity(mActivity, "com.miui.securitycenter",
                "com.miui.permcenter.autostart.AutoStartManagementActivity");
    }

    public void goHuaweiSetting(Activity mActivity) {
        try {
            showActivity(mActivity, "com.huawei.systemmanager",
                    "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
        } catch (Exception e) {
            showActivity(mActivity, "com.huawei.systemmanager",
                    "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
        }
    }

    public void goOPPOSetting(Activity mActivity) {
        try {
            showActivity(mActivity, "com.coloros.phonemanager");
        } catch (Exception e1) {
            try {
                showActivity(mActivity, "com.oppo.safe");
            } catch (Exception e2) {
                try {
                    showActivity(mActivity, "com.coloros.oppoguardelf");
                } catch (Exception e3) {
                    showActivity(mActivity, "com.coloros.safecenter");
                }
            }
        }
    }

    public void goVIVOSetting(Activity mActivity) {
        showActivity(mActivity, "com.iqoo.secure");
    }

    public void goMeizuSetting(Activity mActivity) {
        showActivity(mActivity, "com.meizu.safe");
    }

    public void goSamsungSetting(Activity mActivity) {
        try {
            showActivity(mActivity, "com.samsung.android.sm_cn");
        } catch (Exception e) {
            showActivity(mActivity, "com.samsung.android.sm");
        }
    }
    /***********************跳转各手机品牌的自启管理==结束********************************/
    /**
     * 跳转到指定应用的首页
     */
    private void showActivity(Activity mActivity, @NonNull String packageName) {
        if (mActivity == null) {
            return;
        }
        Intent intent = mActivity.getPackageManager().getLaunchIntentForPackage(packageName);
        mActivity.startActivity(intent);
    }

    /**
     * 跳转到指定应用的指定页面
     */
    private void showActivity(Activity mActivity, @NonNull String packageName, @NonNull String activityDir) {
        if (mActivity == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, activityDir));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mActivity.startActivity(intent);
    }

    /**
     * 获取应用详情页面
     *
     * @return
     */
    private void goToAppDetailSetting(Activity activity) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        localIntent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        activity.startActivity(localIntent);
    }

}
